<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>随声记</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .upload-container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .file-input-wrapper {
            margin-bottom: 20px;
        }
        .upload-btn {
            background-color: #1890ff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        .upload-btn:disabled {
            background-color: #ccc;
            cursor: not-allowed;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 4px;
        }
        .success {
            background-color: #f6ffed;
            border: 1px solid #b7eb8f;
            color: #52c41a;
        }
        .error {
            background-color: #fff2f0;
            border: 1px solid #ffccc7;
            color: #ff4d4f;
        }
        .audio-preview {
            margin-top: 20px;
        }
        .file-info {
            margin-top: 10px;
            color: #666;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="upload-container">
        <h1>音频文件上传测试</h1>
        <div class="file-input-wrapper">
            <input type="file" id="audioFile" accept="audio/*">
            <div class="file-info" id="fileInfo"></div>
        </div>
        <button class="upload-btn" id="uploadBtn" disabled>上传文件</button>
        <div id="result" style="display: none;"></div>
        <div class="audio-preview" id="audioPreview" style="display: none;">
            <h3>音频预览</h3>
            <audio controls id="audioPlayer"></audio>
        </div>
    </div>

    <script>
        const audioFile = document.getElementById('audioFile');
        const uploadBtn = document.getElementById('uploadBtn');
        const result = document.getElementById('result');
        const fileInfo = document.getElementById('fileInfo');
        const audioPreview = document.getElementById('audioPreview');
        const audioPlayer = document.getElementById('audioPlayer');

        // 文件选择事件处理
        audioFile.addEventListener('change', (e) => {
            const file = e.target.files[0];
            if (file) {
                // 验证文件类型
                if (!file.type.startsWith('audio/')) {
                    showError('请选择音频文件');
                    audioFile.value = '';
                    return;
                }

                // 验证文件大小（50MB）
                const maxSize = 50 * 1024 * 1024;
                if (file.size > maxSize) {
                    showError('文件大小不能超过50MB');
                    audioFile.value = '';
                    return;
                }

                // 显示文件信息
                fileInfo.textContent = `文件名：${file.name}\n大小：${(file.size / 1024 / 1024).toFixed(2)}MB`;
                uploadBtn.disabled = false;

                // 预览音频
                const url = URL.createObjectURL(file);
                audioPlayer.src = url;
                audioPreview.style.display = 'block';
            } else {
                uploadBtn.disabled = true;
                audioPreview.style.display = 'none';
            }
        });

        // 上传按钮点击事件
        uploadBtn.addEventListener('click', async () => {
            const file = audioFile.files[0];
            if (!file) return;

            uploadBtn.disabled = true;
            result.style.display = 'none';

            const formData = new FormData();
            formData.append('audio', file);

            try {
                const response = await fetch('/upload/audio', {
                    method: 'POST',
                    body: formData
                });

                const data = await response.json();

                if (response.ok) {
                    showSuccess(`上传成功！<br>文件访问链接：<a href="${data.data.url}" target="_blank">${data.data.url}</a>`);
                } else {
                    showError(data.error || '上传失败');
                }
            } catch (err) {
                showError('上传失败：' + err.message);
            } finally {
                uploadBtn.disabled = false;
            }
        });

        function showSuccess(message) {
            result.className = 'result success';
            result.innerHTML = message;
            result.style.display = 'block';
        }

        function showError(message) {
            result.className = 'result error';
            result.textContent = message;
            result.style.display = 'block';
        }
    </script>
</body>
</html>